﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using VirtuOS.Components;

namespace VirtuOS.Base
{
    class Terminal
    {
       
        public void Vconsole()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(Prompt());
            Console.ResetColor();
            string input = Console.ReadLine();
            while (input != null)
            {
                InputHandler.CommandHandler(input);
                Vconsole();
            }
        }

        private static string Prompt()
        {
            string user = Properties.Settings.Default.USER;
            string ps1 = Properties.Settings.Default.PS1;
            ps1 = ps1.Replace("\\u", user);
            ps1 = ps1.Replace("\\h", Environment.MachineName.ToLower());
            ps1 = ps1.Replace("\\w", FileHandler.CurrentDirectory());
            ps1 = ps1.Replace(Properties.Settings.Default.HOMEDIR, "~");

            if (user.Equals("root"))
            {
                ps1 = ps1.Replace("\\$", "#");
            }
            else
            {
                ps1 = ps1.Replace("\\$", "$");
            }
            return ps1;
        }

        public static void ChangeDir(string dir)
        {
            string cdir = FileHandler.CurrentDirectory();
            string ndir = String.Empty;
            if (dir.StartsWith("/"))
            {
                ndir = VirtuOS.vdir_full + dir;
            }
            else
            {
                ndir = dir;
            }
            if ((ndir.Contains(VirtuOS.vdir_full)) && (Directory.Exists(ndir)))
            {
                Environment.CurrentDirectory = ndir;
                Title("VirtuOS · " + Properties.Settings.Default.USER +
                    " · " + FileHandler.CurrentDirectory());
            }
            else
            {
                Console.WriteLine("Directory {0} out of bounds or does not exist.", dir);
            }
        }

        public static void AsciiArt()
        {
            Console.WriteLine("");
            Console.WriteLine("  888     888 d8b         888              .d88888b.   .d8888b.  ");
            Console.WriteLine("  888     888 Y8P         888             d88P\" \"Y88b d88P  Y88b ");
            Console.WriteLine("  888     888             888             888     888 Y88b.      ");
            Console.WriteLine("  Y88b   d88P 888 888d888 888888 888  888 888     888  \"Y888b.   ");
            Console.WriteLine("   Y88b d88P  888 888P\"   888    888  888 888     888     \"Y88b. ");
            Console.WriteLine("    Y88o88P   888 888     888    888  888 888     888       \"888 ");
            Console.WriteLine("     Y888P    888 888     Y88b.  Y88b 888 Y88b. .d88P Y88b  d88P ");
            Console.WriteLine("      Y8P     888 888      \"Y888  \"Y88888  \"Y88888P\"   \"Y8888P\"  ");
        }

        private static void Title(string s)
        {
            Console.Title = s;
        }
    }
}
